package com.party.admin.web.controller.wechat;

import com.google.common.base.Strings;
import com.party.admin.biz.system.MemberMerchantBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.core.model.wechat.WechatMerchants;
import com.party.core.service.wechat.IWechatMerchantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 微信商户控制器
 * Created by wei.li.
 *
 * @date 2018/1/23 0023
 * @time 17:20
 */

@Controller
@RequestMapping(value = "wechat/merchants")
public class WechatMerchantsController {

    @Autowired
    private IWechatMerchantsService wechatMerchantsService;

    @Autowired
    private MemberMerchantBizService memberMerchantBizService;


    /**
     * 微信商户列表
     *
     * @param wechatMerchants 微信商户信息
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(WechatMerchants wechatMerchants, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/wechat/wechatMerchantsList");
        List<WechatMerchants> list = wechatMerchantsService.listPage(wechatMerchants, page);
        modelAndView.addObject("page", page);
        modelAndView.addObject("list", list);
        return modelAndView;
    }


    /**
     * 查看微信商户信息
     *
     * @param id 编号
     * @return 交互数据
     */
    @RequestMapping(value = "view")
    public ModelAndView view(String id){
        ModelAndView modelAndView = new ModelAndView("/wechat/wechatMerchantsView");
        WechatMerchants wechatMerchants;
        if (Strings.isNullOrEmpty(id)){
            wechatMerchants = new WechatMerchants();
        }
        else {
            wechatMerchants = wechatMerchantsService.get(id);
        }
        modelAndView.addObject("wechatMerchants", wechatMerchants);
        return modelAndView;
    }


    /**
     * 保存微信商户信息
     *
     * @param wechatMerchants 微信商户信息
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxResult save(WechatMerchants wechatMerchants){
        AjaxResult ajaxResult = new AjaxResult(true);
        wechatMerchants.setMerchantId( memberMerchantBizService.trim(wechatMerchants.getMerchantId()));
        wechatMerchants.setMerchantApiKey(memberMerchantBizService.trim(wechatMerchants.getMerchantApiKey()));
        if (Strings.isNullOrEmpty(wechatMerchants.getId())){
            // 保存前先验证
            WechatMerchants old = wechatMerchantsService.findBymerchantId(wechatMerchants.getMerchantId());
            if (null != old && wechatMerchants.getMerchantId().equals(old.getMerchantId())) {
                ajaxResult.setSuccess(false);
                ajaxResult.setDescription("保存微信商户信息失败, merchantId: {0} 的商户已经存在".replace("{0}",
                        old.getMerchantId()));
                return ajaxResult;
            }
            wechatMerchants.setType(Constant.WECHAT_ACCOUNT_TYPE_PARTNER);
            wechatMerchantsService.insert(wechatMerchants);
        }
        else {
            wechatMerchantsService.update(wechatMerchants);
        }
        return ajaxResult;
    }
}
